App.holdings = function() {

  App.fn = App.fn || {};
  App.fn.update_holding_list = function (res) {
    if (res && res.status == 200 && res.data.status == 1) {
        var total = parseInt(res.data.detail);

        total = total > 1 ? total : 10000;

        //console.log(res);
        var num = 0;
        var all = 0;
        res.data.data.data.forEach(function(v) {
          if (++num>22)  return;
          $("#id-holding-list").append(
            "<tr><td>" 
            + v.CAT_NAME + "</td><td class='number'>" 
            +  (100 * v.TOTAL / total).toFixed(3)
            + "%</td><td class='number'>"+ v.TOTAL +"</td></tr>");
          //all += v.TOTAL;
        }) ;
      }
  }

  var update_summary = function() {
    axios.get('stat/summary').then(function(res) {
      var data = res.data.data;
      var el = $('#summary-info');
      var html = '';
      if (data.READERS) {
        $('.title', el).html("读者数量");
        $('.number', el).html(data.READERS);
        html += el.html();
      }

      if (data.HOLDINGS) {
        $('.title', el).html("馆藏数量");
        $('.number', el).html(data.HOLDINGS);
        html += el.html();
      }

      if (data.BOOKINGS) {
        $('.title', el).html("预约数量");
        $('.number', el).html(data.BOOKINGS);
        html += el.html();
      }

      if (data.LIBS) {
        $('.title', el).html("成员馆");
        $('.number', el).html(data.LIBS);
        html += el.html();
      }
      el.html(html);

    });
  }
  update_summary();

  var update_newbooks = function() {
    if ($("table#newbooks-table").size() == 0) {
      return;
    }
    axios.get('stat/holdings4new?size=20').then(function(res){
      if (res && res.data) {
        res.data.data.data.list.forEach(function(item){
          $("table#newbooks-table").append(
            "<tr><td>" + item.Biblio.TITLE + "</td><td>" + item.Biblio.AUTHOR + "</td><td>" + item.Biblio.PUBLISHER + "</td></tr>"
          );
        });
      }
    });
  }
  update_newbooks();

  App.fn.widget_pie_holding = function(res){

    var data =  [
        { label: "Premium Purchases", data: 15 },
        { label: "Standard Plans", data: 55 },
        { label: "Services", data: 30 }
    ];

      //ajax
      if (res && res.status == 200 && res.data.status == 1) {
        data = [];
        var total = parseInt(res.data.detail);
        total = total > 1 ? total : 10000;

        var num = 0;
        var all = 0;
        res.data.data.data.forEach(function(v) {
          if (++num>8)  return;
          all += v.TOTAL;
          data.push({label: v.CAT_NAME, data: (v.TOTAL*100/total) });
        });
        data.push({label: '其他', data: 100*(total-all)/total});
      }
      

      var color1 = tinycolor( App.color.primary ).lighten( 5 ).toString();
      var color2 = App.color.alt2;
      var color3 = App.color.alt1;

      var legendContainer = $("#widget-top-hold").parent().next().find(".legend");

      var labelFormatter = function(label, series) {
        return "<div style='font-size:7pt; text-align:center; padding:1px; color:white; margin:3px'>" + label.substr(0,2) + "<br/>" + Math.round(series.percent) + "%</div>";
      }

      $.plot('#widget-top-hold', data, {
        series: {
          pie: {
            show: true,
            innerRadius: 0.27,
            highlight: {
              opacity: 0.1
            },
            radius: 1,
            label: {
              threshold: 0.04,
              show: true,
                      radius: 0.8,
                      formatter: labelFormatter,
                      background: {
                          opacity: 0.4,
                          color: '#000'
                      }
            }
          }
        },
        grid:{
          hoverable: true,
          clickable: true
        },
        legend:{
          container: legendContainer
        },
          colors: [ color1, color2, color3]  
      });
    };

    //Init
  App.loading('stat/holdings', function(res) {
    App.fn.widget_pie_holding(res);
    App.fn.update_holding_list(res);
  });

  return this;
};
